<?php

namespace App\Modules\Inventory\Controllers;

use App\Lib\Utilities;
use App\Modules\Inventory\Services\GoodsCategoryService;
use App\Modules\Inventory\Validators\GoodsCategoryControllerValidator;
use Flower\Core\Http\BaseController;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class GoodsCategoryController extends BaseController
{
    protected $goodsCategoryService;

    public function __construct(GoodsCategoryService $goodsCategoryService)
    {
        $this->goodsCategoryService = $goodsCategoryService;
    }

    public function getRules()
    {
        return GoodsCategoryControllerValidator::validator();
    }

    /**
     * 新增商品分类
     * @param Request $request
     * @return mixed
     */
    public function createGoodsCategory(Request $request)
    {
        $store_id = $request->input('store_id');
        $parent_id = $request->input('parent_id');
        $name = $request->input('name');

        $goods_category = $this->goodsCategoryService->createGoodsCategory($store_id, $parent_id, $name);

        $data = [
            'goods_category_id' => $goods_category->id
        ];

        return $this->display($data, '添加成功');

    }

    /**
     * 编辑商品分类
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function editGoodsCategory(Request $request)
    {
        $store_id = $request->input('store_id');
        $category_id = $request->input('category_id');
        $name = $request->input('name');

        $this->goodsCategoryService->editGoodsCategory($store_id,$category_id,$name);

        return $this->success('操作成功');

    }

    /**
     * 删除店铺分类
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function delGoodsCategory(Request $request)
    {
        $store_id = $request->input('store_id');
        $category_id = $request->input('category_id');

        $this->goodsCategoryService->delGoodsCategory($store_id,$category_id);

        return $this->success('删除成功');

    }

    /**
     * 获取本店所有分类
     * @param Request $request
     * @return mixed
     */

    public function getGoodsCategoryList(Request $request)
    {
        $store_id = $request->input('store_id');
        $type = $request->input('type');
        $parent_id = $request->input('parent_id');
        $goods_category_list = $this->goodsCategoryService->getGoodsCategoryList($store_id,$type,$parent_id);

        return $this->display($goods_category_list, '返回成功');

    }
}
